package top.ochiamalu.aioj.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import top.ochiamalu.aioj.interceptor.WsHandshakeInterceptor;
import top.ochiamalu.aioj.websocket.JudgeHandler;

import javax.annotation.Resource;

/**
 * websocket配置
 *
 * @author ochiamalu
 * @date 2025/01/23
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Resource
    private WsHandshakeInterceptor wsHandshakeInterceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(judgeHandler(), "/ws/judge")
                .addInterceptors(wsHandshakeInterceptor)
                .setAllowedOrigins("*");
    }

    @Bean
    public JudgeHandler judgeHandler() {
        return new JudgeHandler();
    }
}
